<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="上报类别" prop="infoType">
        <el-select
          v-model="queryParams.infoType"
          clearable
          placeholder="请选择上报类别"
        >
          <el-option
            v-for="dict in queryRoomList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            size="small"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发起单位" prop="schoolCode">
        <el-select
          v-model="queryParams.schoolCode"
          clearable
          placeholder="请选择发起单位"
        >
          <el-option
            v-for="dict in schoolSelect"
            :key="dict.code"
            :label="dict.title"
            :value="dict.code"
            size="small"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      size="small"
      icon="el-icon-plus"
      @click="showAddBtn"
      >新增</el-button
    >
    <el-button
      type="primary"
      size="small"
      icon="el-icon-delete"
      @click="handleDelete"
      >删除</el-button
    >
    <el-table
      v-loading="loading"
      :data="studentCheckList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        label="序号"
        align="center"
        :index="customeIndex"
        width="80"
      ></el-table-column>
      <el-table-column label="标题" align="center" prop="infoTitle" />
      <el-table-column label="上报类别" align="center" prop="infoType" :formatter="schoolTypeFormat"/>
      <el-table-column label="发起单位" align="center" prop="schoolName" />
      <el-table-column label="上报人" align="center" prop="userName" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改宿舍考勤对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="标题" prop="infoTitle">
            <el-input
              v-model="ruleForm.infoTitle"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="上报类别" prop="infoType">
            <el-select
              v-model="ruleForm.infoType"
              clearable
              placeholder="请选择上报类别"
            >
              <el-option
                v-for="dict in queryRoomList"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
                size="small"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input
              type="textarea"
              v-model="ruleForm.content"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" size="small"
          >确 定</el-button
        >
        <el-button @click="cancel" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import { uploadList, uploadListAdd, updateUpload,delUpload,schoolList } from "@/api/qu/ysgl";
import { selectRoom } from "@/api/base/room";
import { getToken } from "@/utils/auth";
import { selectStudent } from "@/api/base/student";
import { Message } from "element-ui";

export default {
  name: "StudentCheck",
  data() {
    return {
      school: {
        name: "张三",
        age: "5",
        sex: "男",
        class: "小二班",
        father: "张一",
        mother: "王五",
        medicalHistory: "关于病史的信息",
      },
      schoolNatureOptions: [],
      leveltypeOptions: [],
      // 遮罩层
      loading: false,
      total: 0,
      // 宿舍数据
      roomList: [],
      queryRoomList: [],
      //学生数据
      studentCheckList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      ruleForm: {
        schoolCode: this.$store.getters.schoolCode,
        userId: this.$store.getters.userId,
      },
      title: "",
      rules: {
        infoTitle: [{ required: true, message: "请输入标题", trigger: "blur" }],
        queryRoomList: [
          { required: true, message: "请选择上报类别", trigger: "blur" },
        ],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      schoolSelect:[]
    };
  },
  created() {
    this.getList();
    this.schoolList()
    this.getDicts("info_upload_type").then((response) => {
      this.queryRoomList = response.data;
    });
  },
  methods: {
    async getList() {
      try {
        const obj = {
          ...this.queryParams
        };
        const { rows, code, msg, total } = await uploadList(obj);
        this.studentCheckList = rows;
        this.total = total;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        console.log(error);
      }
    },
    // 序号
    customeIndex(index) {
      return (
        index + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    handleUpdate(row) {
      this.title = "修改上报信息";
      this.ruleForm = { ...row };
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams,'-----this.queryParams');
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        infoType: null, // 上报类别
        schoolCode: null, // 发起单位
      }),
        this.resetForm("queryForm");
      this.handleQuery();
    },
    // 园所性质字典翻译
    schoolTypeFormat(row, column) {
      return this.selectDictLabel(this.queryRoomList, row.infoType);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    async submitForm() {
      this.loading = true;
      const { code, msg } = !this.ruleForm.id
        ? await uploadListAdd(this.ruleForm)
        : await updateUpload(this.ruleForm);
      if (code == 200) {
        this.loading = false;
        this.open = false;
        Message.success(!this.ruleForm.id ? "新增成功" : "修改成功");
        this.handleQuery();
      } else {
        this.$message.error(msg);
        this.loading = false;
      }
    },
    // 显示添加弹窗
    showAddBtn() {
      this.ruleForm = {}
      this.$refs.ruleForm.resetFields();
      this.title = "新增上报信息";
      this.open = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除信息上报编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUpload(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
   async schoolList(){
    const {rows,code,msg} = await schoolList()
    if(code == 200){
      this.schoolSelect = rows
    }else{
      this.$message.error(msg)
    }
    },
    // /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm('是否确认导出所有宿舍考勤数据项?', "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(function() {
    //     return exportStudentCheck(queryParams);
    //   }).then(response => {
    //     this.download(response.msg);
    //   })
    // },
  },
};
</script>
